package com.p2p.front.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.base.page.PageVo;
import com.p2p.front.dal.dao.WithdrawDao;
import com.p2p.front.dal.model.Withdraw;
import com.p2p.front.service.WithdrawService;
import com.p2p.trans.client.enums.FreezeBalanceTypeEnum;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.model.AccountVO;
import com.p2p.trans.client.model.TransInfo;

/**
 * leiwen@yiqihao.com
 */
@Service
public class WithdrawServiceImpl implements WithdrawService {

	@Resource
	private WithdrawDao withdrawDao;

	public PageVo listPage(PageVo form) {
		List<Map<String, Object>> list = withdrawDao.listPage(form);
		form.setList(list);
		return form;
	}
	
	@Resource 
	private AccountTransFacade accountTrans;
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void save(Withdraw withdraw,AccountVO accountVO) {
		withdrawDao.save(withdraw);
		
		//远程接口放在最后调用，不能回滚（目前不支持全站、分布式事务）
		TransInfo transInfo = new TransInfo();
		//冻结账户余额
		transInfo.setTransAccountIn(accountVO.getAccountNo());		
		transInfo.setTransAmount(withdraw.getMoney());
		transInfo.setFreezeType(FreezeBalanceTypeEnum.FREEZE_FOR_WITHDRAW);
		
		accountTrans.doFreeze(transInfo);
		
	}
}
